﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Sumc.Client.ModernUI.Common;
using Sumc.Client.ModernUI.Extensions;
using Sumc.Client.ModernUI.Helpers;
using Sumc.Client.ModernUI.Models;
using Sumc.Client.ModernUI.Services;
using Sumc.Models.Responses;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml.Navigation;

namespace Sumc.Client.ModernUI.ViewModels
{
    public class StopPageViewModel : BaseViewModel
    {
        private bool isFound;
        private string information;
        private Stop stop;
        private DelegateCommand<string> goToSchedule;
        private DelegateCommand<string> loadStop;
        private ObservableCollection<Stop> foundStops;
        private ObservableCollection<VehicleVirtualTable> buses;
        private ObservableCollection<VehicleVirtualTable> trolleys;
        private ObservableCollection<VehicleVirtualTable> trams;
        private readonly IVirtualTableRepository virtualTableRepository;

        public StopPageViewModel(INavigationService navigationService,
            IAlertMessageService alertMessageService,
            IVirtualTableRepository virtualTableRepository,
            ISearchPaneService searchPaneService,
            IFlyoutService flyoutService)
            : base(navigationService,
            alertMessageService,
            flyoutService,
            searchPaneService)
        {
            this.virtualTableRepository = virtualTableRepository;
        }

        public DelegateCommand<string> GoToSchedule
        {
            get
            {
                if (this.goToSchedule == null)
                {
                    this.goToSchedule = new DelegateCommand<string>(this.NavigateToSchedule);
                }

                return this.goToSchedule;
            }
        }

        public DelegateCommand<string> LoadStop
        {
            get
            {
                if (this.loadStop == null)
                {
                    this.loadStop = new DelegateCommand<string>(this.LoadStopInformation);
                }

                return this.loadStop;
            }
        }

        public bool IsFound
        {
            get
            {
                return this.isFound;
            }

            set
            {
                this.SetProperty(ref this.isFound, value);
            }
        }

        public string Information
        {
            get
            {
                return this.information;
            }

            set
            {
                this.SetProperty(ref this.information, value);
            }
        }

        public Stop Stop
        {
            get
            {
                return this.stop;
            }

            set
            {
                this.SetProperty(ref this.stop, value);
            }
        }

        public ObservableCollection<Stop> FoundStops
        {
            get
            {
                return this.foundStops;
            }

            set
            {
                this.SetProperty(ref this.foundStops, value);
            }
        }

        public ObservableCollection<VehicleVirtualTable> Buses
        {
            get
            {
                return this.buses;
            }

            set
            {
                this.SetProperty(ref this.buses, value);
            }
        }

        public ObservableCollection<VehicleVirtualTable> Trams
        {
            get
            {
                return this.trams;
            }

            set
            {
                this.SetProperty(ref this.trams, value);
            }
        }

        public ObservableCollection<VehicleVirtualTable> Trolleys
        {
            get
            {
                return this.trolleys;
            }

            set
            {
                this.SetProperty(ref this.trolleys, value);
            }
        }

        private void NavigateToSchedule(string scheduleUrl)
        {
            this.navigationService.Navigate(Constants.SchedulePage, scheduleUrl);
        }

        private void LoadStopInformation(string stop)
        {
            this.LoadData(async () =>
            {
                var virtualTable = await this.virtualTableRepository.GetStopVirtualTable(stop);
                this.LoadingData = false;
                this.Information = virtualTable.Information;
                this.Stop = virtualTable.Stop;
                if (virtualTable.FoundStops != null && virtualTable.FoundStops.FirstOrDefault() != null)
                {
                    this.FoundStops = new ObservableCollection<Stop>();
                    this.FoundStops.FillCollection(virtualTable.FoundStops);
                    this.IsFound = true;
                }

                if (virtualTable.Buses != null)
                {
                    this.Buses = new ObservableCollection<VehicleVirtualTable>();
                    this.Buses.FillCollection(virtualTable.Buses);
                    this.IsFound = true;
                }

                if (virtualTable.Trams != null)
                {
                    this.Trams = new ObservableCollection<VehicleVirtualTable>();
                    this.Trams.FillCollection(virtualTable.Trams);
                    this.IsFound = true;
                }

                if (virtualTable.Trolleys != null)
                {
                    this.Trolleys = new ObservableCollection<VehicleVirtualTable>();
                    this.Trolleys.FillCollection(virtualTable.Trolleys);
                    this.IsFound = true;
                }
            });
        }

        public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            this.LoadStopInformation(navigationParameter as string);
        }
    }
}